package com.asteros.crypto.config;

import com.asteros.crypto.utils.services.CRLServiceConfig;
import com.asteros.crypto.utils.services.CryptoServiceConfig;
import com.asteros.crypto.utils.services.TspServerConfig;

import java.util.Properties;

public final class AppConfig
{
	public static final String TSP_SERVER_URL 	= "tsp.server.url";
	public static final String CRL_SERVER_URL 	= "crl.server.url";
	public static final String OCSP_SERVER_URL 	= "ocsp.server.url";
	
	public static final String PROVIDER 		= "crypto.provider";
	public static final String STORE_TYPE 		= "crypto.storetype";
	public static final String STORAGE_TYPE 	= "crypto.storagetype";
	
	public static final String CONTAINER_NAME 	= "keystore.container";
	
	public static final String ETOKEN_PASSWORD 	= "keystore.etoken.password";
	public static final String RUTOKEN_PASSWORD = "keystore.rutoken.password";
	public static final String PKCS12_PASSWORD 	= "keystore.pkcs12.password";

	public static final String FS 				= "file.separator";
	public static final String STORE_PATH 		= "keystore.path";
	public static final String SEED_PATH 		= "keystore.seedpath";

	public static final String PKCS12_PFX 		= "keystore.pkcs12.pfx";


	private static AppConfig instance = new AppConfig();

	private Properties conf = null;

	public static AppConfig getInstance()
	{
		
		return instance;
	}

	public void init(Properties conf) {
		this.conf = conf;
		if (conf.getProperty(FS) == "") {
			this.conf.put(FS,System.getProperty(FS));
		} 
	}

	public void defaultClientRuTokenInit()
	{
		System.out.println("AppConfig.defaultInit>>");
		this.conf = new Properties();

		String documentID = "123";
		conf.put("documentID", documentID);
		
		this.conf.put(TSP_SERVER_URL, "http://www.cryptopro.ru/tsp/tsp.srf");
		this.conf.put(CRL_SERVER_URL, "blablabla");
		this.conf.put(OCSP_SERVER_URL, "");

		
		this.conf.put(PROVIDER, "SC");
		this.conf.put(STORE_TYPE, "PKCS12");
		this.conf.put(STORAGE_TYPE, "RUTOKEN");
		this.conf.put(CONTAINER_NAME, "Test key");
		this.conf.put(ETOKEN_PASSWORD, "Q1w2e3r4");
		this.conf.put(RUTOKEN_PASSWORD, "12345678");
		this.conf.put(PKCS12_PASSWORD, "password");

		String seed = "rutoken 0:";				// seed 
		String path = "rutoken 0:/0001";		// path to pfx
		this.conf.put(SEED_PATH, seed);
		this.conf.put(STORE_PATH, path);
		this.conf.put(FS, "/");					// ��� �������� ������ "/"
		this.conf.put(PKCS12_PFX, "0001");		

		System.out.println("AppConfig.defaultInit<<");
	}
	
	public void defaultClientInit()
	{
		System.out.println("AppConfig.defaultInit>>");

		this.conf = new Properties();

		this.conf.put(TSP_SERVER_URL, "http://www.cryptopro.ru/tsp/tsp.srf");
		this.conf.put(CRL_SERVER_URL, "blablabla");
		this.conf.put(OCSP_SERVER_URL, "");

		this.conf.put(PROVIDER, "SC");
		this.conf.put(STORE_TYPE, "PKCS12");
		this.conf.put(STORAGE_TYPE, "FS");
		this.conf.put(CONTAINER_NAME, "Test key");
		this.conf.put(ETOKEN_PASSWORD, "Q1w2e3r4");
		this.conf.put(RUTOKEN_PASSWORD, "12345678");
		this.conf.put(PKCS12_PASSWORD, "password");
//		this.conf.put(STORE_PATH, "C:\\Projects\\RKK\\testdata\\alice");
		this.conf.put(STORE_PATH, "C:\\Projects\\RKK\\testdata\\appserver1");
		this.conf.put(SEED_PATH, "C:\\Projects\\RKK\\testdata\\appserver1");
		this.conf.put(PKCS12_PFX, "store.pfx");		
		this.conf.put(FS, System.getProperty(FS));

		System.out.println("AppConfig.defaultInit<<");
	}

	public void defaultServerInit()
	{
		System.out.println("AppConfig.defaultInit>>");
	
		this.conf = new Properties();
	
		this.conf.put(TSP_SERVER_URL, "http://www.cryptopro.ru/tsp/tsp.srf");
		this.conf.put(CRL_SERVER_URL, "blablabla");
		this.conf.put(OCSP_SERVER_URL, "");

		this.conf.put(PROVIDER, "SC");
		this.conf.put(STORE_TYPE, "PKCS12");
		this.conf.put(STORAGE_TYPE, "FS");
		this.conf.put(CONTAINER_NAME, "Test key");
		this.conf.put(ETOKEN_PASSWORD, "Q1w2e3r4");
		this.conf.put(RUTOKEN_PASSWORD, "12345678");
		this.conf.put(PKCS12_PASSWORD, "password");
		this.conf.put(STORE_PATH, "C:\\Projects\\RKK\\testdata\\appserver");
		this.conf.put(SEED_PATH, "C:\\Projects\\RKK\\testdata\\appserver");
		this.conf.put(FS, System.getProperty(FS));

		this.conf.put(PKCS12_PFX, "store.pfx");		
	
		System.out.println("AppConfig.defaultInit<<");
	}

	public String getProperty(String key) {
		return this.conf.getProperty(key);
	}

	public CRLServiceConfig getCRLServerConfig()
	{
		try {
			CRLServiceConfig config = new CRLServiceConfig(this.conf.getProperty("crl.server.url"));
			return config;
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return null;
	}
	
	public TspServerConfig getTspServerConfig()
	{
		try {
			TspServerConfig config = new TspServerConfig(this.conf.getProperty("tsp.server.url"));
			return config;
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return null;
	}

	public CryptoServiceConfig getSCPKCS12CryptoServiceConfig()
	{
		CryptoServiceConfig config = new CryptoServiceConfig(this.conf.getProperty(PKCS12_PASSWORD), "PKCS12");
		config.setProvider(this.conf.getProperty(PROVIDER));

		config.setStorageType(this.conf.getProperty(STORAGE_TYPE));
		config.setSeedPath(this.conf.getProperty(SEED_PATH));
		config.setStorePath(this.conf.getProperty(STORE_PATH) + this.conf.getProperty(FS) + this.conf.getProperty(PKCS12_PFX));
		config.setContainerName(this.conf.getProperty(CONTAINER_NAME));

		config.setTspServerConfig(getTspServerConfig());
		config.setCrlServiceConfig(getCRLServerConfig());

		return config;
	}

	public CryptoServiceConfig getRutokenCryptoServiceConfig()
	{
		return getRutokenCryptoServiceConfig(this.conf.getProperty(PKCS12_PASSWORD)); 
	}

	public CryptoServiceConfig getRutokenCryptoServiceConfig(String password)
	{
		CryptoServiceConfig config = new CryptoServiceConfig(password, "PKCS12");
		//CryptoServiceConfig config = new CryptoServiceConfig(password, "RutokenStore");
		config.setStorageType(this.conf.getProperty(STORAGE_TYPE));
		config.setProvider(this.conf.getProperty(PROVIDER));
		config.setSeedPath(this.conf.getProperty(SEED_PATH));
		config.setStorePath(this.conf.getProperty(STORE_PATH) + this.conf.getProperty(FS) + this.conf.getProperty(PKCS12_PFX));
		config.setContainerName(this.conf.getProperty(CONTAINER_NAME));

		config.setTspServerConfig(getTspServerConfig());
		config.setCrlServiceConfig(getCRLServerConfig());
		
		return config; 
	}

	public CryptoServiceConfig getEtokenCryptoServiceConfig()
	{
		CryptoServiceConfig config = new CryptoServiceConfig(this.conf.getProperty("keystore.etoken.password"), "OCFStore");

		config.setTspServerConfig(getTspServerConfig());
		config.setCrlServiceConfig(getCRLServerConfig());
		
		return config;
	}

	public CryptoServiceConfig getEtokenCryptoServiceConfig(String password) {
		CryptoServiceConfig config = new CryptoServiceConfig(password, "OCFStore");

		config.setTspServerConfig(getTspServerConfig());
		config.setCrlServiceConfig(getCRLServerConfig());
	
		return config;
	}
}
